﻿using System;
using System.Collections.Generic;

using System.Text;

namespace System.Qizx.Xml
{
    /*******************************/
    /// <summary>
    /// This class will manage all the parsing operations emulating the SAX parser behavior
    /// </summary>
    public class SaxAttributesSupport
    {
        private System.Collections.ArrayList MainList;

        /// <summary>
        /// Builds a new instance of SaxAttributesSupport.
        /// </summary>
        public SaxAttributesSupport()
        {
            MainList = new System.Collections.ArrayList();
        }

        /// <summary>
        /// Creates a new instance of SaxAttributesSupport from an ArrayList of Att_Instance class.
        /// </summary>
        /// <param name="arrayList">An ArraList of Att_Instance class instances.</param>
        /// <returns>A new instance of SaxAttributesSupport</returns>
        public SaxAttributesSupport(SaxAttributesSupport List)
        {
            SaxAttributesSupport temp = new SaxAttributesSupport();
            temp.MainList = (System.Collections.ArrayList)List.MainList.Clone();
        }

        /// <summary>
        /// Adds a new attribute elment to the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="Uri">The Uri of the attribute to be added.</param>
        /// <param name="Lname">The Local name of the attribute to be added.</param>
        /// <param name="Qname">The Long(qualify) name of the attribute to be added.</param>
        /// <param name="Type">The type of the attribute to be added.</param>
        /// <param name="Value">The value of the attribute to be added.</param>
        public virtual void Add(string Uri, string Lname, string Qname, string Type, string Value)
        {
            Att_Instance temp_Attributes = new Att_Instance(Uri, Lname, Qname, Type, Value);
            MainList.Add(temp_Attributes);
        }

        /// <summary>
        /// Clears the list of attributes in the given AttributesSupport instance.
        /// </summary>
        public virtual void Clear()
        {
            MainList.Clear();
        }

        /// <summary>
        /// Obtains the index of an attribute of the AttributeSupport from its qualified (long) name.
        /// </summary>
        /// <param name="Qname">The qualified name of the attribute to search.</param>
        /// <returns>An zero-based index of the attribute if it is found, otherwise it returns -1.</returns>
        public virtual int GetIndex(string Qname)
        {
            int index = GetLength() - 1;
            while ((index >= 0) && !(((Att_Instance)(MainList[index])).att_fullName.Equals(Qname)))
                index--;
            if (index >= 0)
                return index;
            else
                return -1;
        }

        /// <summary>
        /// Obtains the index of an attribute of the AttributeSupport from its namespace URI and its localname.
        /// </summary>
        /// <param name="Uri">The namespace URI of the attribute to search.</param>
        /// <param name="Lname">The local name of the attribute to search.</param>
        /// <returns>An zero-based index of the attribute if it is found, otherwise it returns -1.</returns>
        public virtual int GetIndex(string Uri, string Lname)
        {
            int index = GetLength() - 1;
            while ((index >= 0) && !(((Att_Instance)(MainList[index])).att_localName.Equals(Lname) && ((Att_Instance)(MainList[index])).att_URI.Equals(Uri)))
                index--;
            if (index >= 0)
                return index;
            else
                return -1;
        }

        /// <summary>
        /// Returns the number of attributes saved in the SaxAttributesSupport instance.
        /// </summary>
        /// <returns>The number of elements in the given SaxAttributesSupport instance.</returns>
        public virtual int GetLength()
        {
            return MainList.Count;
        }

        /// <summary>
        /// Returns the local name of the attribute in the given SaxAttributesSupport instance that indicates the given index.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <returns>The local name of the attribute indicated by the index or null if the index is out of bounds.</returns>
        public virtual string GetLocalName(int index)
        {
            try
            {
                return ((Att_Instance)MainList[index]).att_localName;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Returns the qualified name of the attribute in the given SaxAttributesSupport instance that indicates the given index.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <returns>The qualified name of the attribute indicated by the index or null if the index is out of bounds.</returns>
        public virtual string GetFullName(int index)
        {
            try
            {
                return ((Att_Instance)MainList[index]).att_fullName;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Returns the type of the attribute in the given SaxAttributesSupport instance that indicates the given index.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <returns>The type of the attribute indicated by the index or null if the index is out of bounds.</returns>
        public virtual string GetType(int index)
        {
            try
            {
                return ((Att_Instance)MainList[index]).att_type;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Returns the namespace URI of the attribute in the given SaxAttributesSupport instance that indicates the given index.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <returns>The namespace URI of the attribute indicated by the index or null if the index is out of bounds.</returns>
        public virtual string GetURI(int index)
        {
            try
            {
                return ((Att_Instance)MainList[index]).att_URI;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Returns the value of the attribute in the given SaxAttributesSupport instance that indicates the given index.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <returns>The value of the attribute indicated by the index or null if the index is out of bounds.</returns>
        public virtual string GetValue(int index)
        {
            try
            {
                return ((Att_Instance)MainList[index]).att_value;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Modifies the local name of the attribute in the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <param name="LocalName">The new Local name for the attribute.</param>
        public virtual void SetLocalName(int index, string LocalName)
        {
            ((Att_Instance)MainList[index]).att_localName = LocalName;
        }

        /// <summary>
        /// Modifies the qualified name of the attribute in the given SaxAttributesSupport instance.
        /// </summary>	
        /// <param name="index">The attribute index.</param>
        /// <param name="FullName">The new qualified name for the attribute.</param>
        public virtual void SetFullName(int index, string FullName)
        {
            ((Att_Instance)MainList[index]).att_fullName = FullName;
        }

        /// <summary>
        /// Modifies the type of the attribute in the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <param name="Type">The new type for the attribute.</param>
        public virtual void SetType(int index, string Type)
        {
            ((Att_Instance)MainList[index]).att_type = Type;
        }

        /// <summary>
        /// Modifies the namespace URI of the attribute in the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <param name="URI">The new namespace URI for the attribute.</param>
        public virtual void SetURI(int index, string URI)
        {
            ((Att_Instance)MainList[index]).att_URI = URI;
        }

        /// <summary>
        /// Modifies the value of the attribute in the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="index">The attribute index.</param>
        /// <param name="Value">The new value for the attribute.</param>
        public virtual void SetValue(int index, string Value)
        {
            ((Att_Instance)MainList[index]).att_value = Value;
        }

        /// <summary>
        /// This method eliminates the Att_Instance instance at the specified index.
        /// </summary>
        /// <param name="index">The index of the attribute.</param>
        public virtual void RemoveAttribute(int index)
        {
            try
            {
                MainList.RemoveAt(index);
            }
            catch (System.ArgumentOutOfRangeException)
            {
                throw new System.IndexOutOfRangeException("The index is out of range");
            }
        }

        /// <summary>
        /// This method eliminates the Att_Instance instance in the specified index.
        /// </summary>
        /// <param name="indexName">The index name of the attribute.</param>
        public virtual void RemoveAttribute(string indexName)
        {
            try
            {
                int pos = GetLength() - 1;
                while ((pos >= 0) && !(((Att_Instance)(MainList[pos])).att_localName.Equals(indexName)))
                    pos--;
                if (pos >= 0)
                    MainList.RemoveAt(pos);
            }
            catch (System.ArgumentOutOfRangeException)
            {
                throw new System.IndexOutOfRangeException("The index is out of range");
            }
        }

        /// <summary>
        /// Replaces an Att_Instance in the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="index">The index of the attribute.</param>
        /// <param name="Uri">The namespace URI of the new Att_Instance.</param>
        /// <param name="Lname">The local name of the new Att_Instance.</param>
        /// <param name="Qname">The namespace URI of the new Att_Instance.</param>
        /// <param name="Type">The type of the new Att_Instance.</param>
        /// <param name="Value">The value of the new Att_Instance.</param>
        public virtual void SetAttribute(int index, string Uri, string Lname, string Qname, string Type, string Value)
        {
            MainList[index] = new Att_Instance(Uri, Lname, Qname, Type, Value);
        }

        /// <summary>
        /// Replaces all the list of Att_Instance of the given SaxAttributesSupport instance.
        /// </summary>
        /// <param name="Source">The source SaxAttributesSupport instance.</param>
        public virtual void SetAttributes(SaxAttributesSupport Source)
        {
            MainList = Source.MainList;
        }

        /// <summary>
        /// Returns the type of the Attribute that match with the given qualified name.
        /// </summary>
        /// <param name="Qname">The qualified name of the attribute to search.</param>
        /// <returns>The type of the attribute if it exist otherwise returns null.</returns>
        public virtual string GetType(string Qname)
        {
            int temp_Index = GetIndex(Qname);
            if (temp_Index != -1)
                return ((Att_Instance)MainList[temp_Index]).att_type;
            else
                return "";
        }

        /// <summary>
        /// Returns the type of the Attribute that match with the given namespace URI and local name.
        /// </summary>
        /// <param name="Uri">The namespace URI of the attribute to search.</param>
        /// <param name="Lname">The local name of the attribute to search.</param>
        /// <returns>The type of the attribute if it exist otherwise returns null.</returns>
        public virtual string GetType(string Uri, string Lname)
        {
            int temp_Index = GetIndex(Uri, Lname);
            if (temp_Index != -1)
                return ((Att_Instance)MainList[temp_Index]).att_type;
            else
                return "";
        }

        /// <summary>
        /// Returns the value of the Attribute that match with the given qualified name.
        /// </summary>
        /// <param name="Qname">The qualified name of the attribute to search.</param>
        /// <returns>The value of the attribute if it exist otherwise returns null.</returns>
        public virtual string GetValue(string Qname)
        {
            int temp_Index = GetIndex(Qname);
            if (temp_Index != -1)
                return ((Att_Instance)MainList[temp_Index]).att_value;
            else
                return "";
        }

        /// <summary>
        /// Returns the value of the Attribute that match with the given namespace URI and local name.
        /// </summary>
        /// <param name="Uri">The namespace URI of the attribute to search.</param>
        /// <param name="Lname">The local name of the attribute to search.</param>
        /// <returns>The value of the attribute if it exist otherwise returns null.</returns>
        public virtual string GetValue(string Uri, string Lname)
        {
            int temp_Index = GetIndex(Uri, Lname);
            if (temp_Index != -1)
                return ((Att_Instance)MainList[temp_Index]).att_value;
            else
                return "";
        }

        /*******************************/
        /// <summary>
        /// This class is created to save the information of each attributes in the SaxAttributesSupport.
        /// </summary>
        public class Att_Instance
        {
            public string att_URI;
            public string att_localName;
            public string att_fullName;
            public string att_type;
            public string att_value;

            /// <summary>
            /// This is the constructor of the Att_Instance
            /// </summary>
            /// <param name="Uri">The namespace URI of the attribute</param>
            /// <param name="Lname">The local name of the attribute</param>
            /// <param name="Qname">The long(Qualify) name of attribute</param>
            /// <param name="Type">The type of the attribute</param>
            /// <param name="Value">The value of the attribute</param>
            public Att_Instance(string Uri, string Lname, string Qname, string Type, string Value)
            {
                this.att_URI = Uri;
                this.att_localName = Lname;
                this.att_fullName = Qname;
                this.att_type = Type;
                this.att_value = Value;
            }
        }
    }
}
